<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/atom.xml" title="Likianta Blog" type="application/atom+xml" />






<meta name="description" content="1. 先看一下最终的效果图 如图所示，标题栏有两个文字标签和一个指示器，点击可以左右切换，下面空白的地方装载分页，滑动可以左右切换。这是本章要做出来的功能。">
<meta property="og:type" content="article">
<meta property="og:title" content="02 Anykey左右滑动布局ViewPager">
<meta property="og:url" content="https://likianta.coding.me/2017/1123203012/index.html">
<meta property="og:site_name" content="Likianta Blog">
<meta property="og:description" content="1. 先看一下最终的效果图 如图所示，标题栏有两个文字标签和一个指示器，点击可以左右切换，下面空白的地方装载分页，滑动可以左右切换。这是本章要做出来的功能。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/17-11-23/372332.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/17-11-23/12576326.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/17-11-23/45054082.jpg">
<meta property="og:updated_time" content="2017-12-31T15:25:28.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="02 Anykey左右滑动布局ViewPager">
<meta name="twitter:description" content="1. 先看一下最终的效果图 如图所示，标题栏有两个文字标签和一个指示器，点击可以左右切换，下面空白的地方装载分页，滑动可以左右切换。这是本章要做出来的功能。">
<meta name="twitter:image" content="http://ozurciydg.bkt.clouddn.com/17-11-23/372332.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://likianta.coding.me/2017/1123203012/"/>





  <title>02 Anykey左右滑动布局ViewPager | Likianta Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Likianta Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tutorials">
          <a href="/tags/tutorial-catalog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            教程
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://likianta.coding.me/2017/1123203012/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Likianta">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://ozurciydg.bkt.clouddn.com/18-2-12/72826549.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Likianta Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">02 Anykey左右滑动布局ViewPager</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-11-23T20:30:12+08:00">
                2017-11-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/passport-pandora/" itemprop="url" rel="index">
                    <span itemprop="name">从零开发Anykey</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,622
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  12
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="1-先看一下最终的效果图"><a href="#1-先看一下最终的效果图" class="headerlink" title="1. 先看一下最终的效果图"></a>1. 先看一下最终的效果图</h1><p><img src="http://ozurciydg.bkt.clouddn.com/17-11-23/372332.jpg" alt="layout_main标题栏效果图"></p>
<p>如图所示，标题栏有两个文字标签和一个指示器，点击可以左右切换，下面空白的地方装载分页，滑动可以左右切换。这是本章要做出来的功能。</p>
<a id="more"></a>
<h1 id="2-制作主界面layout-main-xml"><a href="#2-制作主界面layout-main-xml" class="headerlink" title="2. 制作主界面layout_main.xml"></a>2. 制作主界面layout_main.xml</h1><p>在主界面中，包含了一个顶部的标签栏以及一个可以装载子页面的ViewPager布局。代码如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">RelativeLayout</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:app</span>=<span class="string">"http://schemas.android.com/apk/res-auto"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">"http://schemas.android.com/tools"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">tools:context</span>=<span class="string">"com.likianta.anykey.MainActivity"</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">&lt;!--LinearLayout代替原标题栏的位置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/linearLayout"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"?attr/actionBarSize"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--?attr/actionBarSize表示匹配原actionBar高度--&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">&lt;!--显示两个标题--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">"@+id/title_all"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"100dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_gravity</span>=<span class="string">"center"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_weight</span>=<span class="string">"1.0"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:gravity</span>=<span class="string">"center_vertical|right"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:padding</span>=<span class="string">"15dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:text</span>=<span class="string">"全部"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:textColor</span>=<span class="string">"@color/mycolorText1"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:textSize</span>=<span class="string">"20dp"</span> /&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">"@+id/title_new"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"100dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_gravity</span>=<span class="string">"center"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_weight</span>=<span class="string">"1.0"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:gravity</span>=<span class="string">"center_vertical|left"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:padding</span>=<span class="string">"15dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:text</span>=<span class="string">"新增"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:textColor</span>=<span class="string">"@color/mycolorText2"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:textSize</span>=<span class="string">"20dp"</span> /&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--这里设置的两个文本的初始状态是文字1是深色#000000，文字2是浅色#8e8e8e。</span></span><br><span class="line"><span class="comment">        之后会在函数中实现点击变色的功能--&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">&lt;!--制作线条，line1是标题栏下的阴影--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/shadow_line1"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"1dp"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_alignBottom</span>=<span class="string">"@id/linearLayout"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:background</span>=<span class="string">"@color/mycolorLineShadow"</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">&lt;!--indicator是紧贴着文字下面的指示条--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">ImageView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/indicator"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"5dp"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_above</span>=<span class="string">"@id/shadow_line1"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:scaleType</span>=<span class="string">"matrix"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:src</span>=<span class="string">"@drawable/scrollbar"</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">&lt;!--制作主体部分viewpager--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">android.support.v4.view.ViewPager</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/viewPager"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_below</span>=<span class="string">"@id/shadow_line1"</span>&gt;</span></span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">    <span class="tag">&lt;/<span class="name">android.support.v4.view.ViewPager</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">RelativeLayout</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><span id="return1">其中文字颜色我设置成了自建样式</span>，具体代码可在本文末的《5.2.更新修复》中看到（<a href="#jump1">点此跳转</a>）。</p>
<p>另外，<code>ImageView</code>里面用到的图片：<code>@drawable/scrollbar</code>，这个是我随便截了一个矩形图片然后Ctrl+C -&gt; Ctrl+V进来的</p>
<h1 id="3-然后制作两个分页"><a href="#3-然后制作两个分页" class="headerlink" title="3. 然后制作两个分页"></a>3. 然后制作两个分页</h1><p>“全部”标签对应的分页：page_item_all.xml<br>“新增”标签对应的分页：page_item_new.xml</p>
<p>在左分页当中，我们要加载一个卡片列表，所以采用RecyclerView布局。<br>左分页page_item_all.xml代码如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">android.support.v7.widget.RecyclerView</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">"http://schemas.android.com/tools"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:id</span>=<span class="string">"@+id/recyclerView"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:background</span>=<span class="string">"#3e3e3e"</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">android.support.v7.widget.RecyclerView</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>在右分页当中，我们要制作表单填写的功能，这些内容会在下章讲，所以现在先留空。<br>右分页page_item_new.xml代码如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">LinearLayout</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:background</span>=<span class="string">"#8e8e8e"</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>完成了两个分页的制作，接下来开始处理滑动逻辑，下面开始是本章的重点内容。</p>
<h1 id="4-MainActivity代码"><a href="#4-MainActivity代码" class="headerlink" title="4. MainActivity代码"></a>4. MainActivity代码</h1><p>在MainActivity.java中，我们主要思考以下要做的事情：</p>
<ol>
<li>加载主界面</li>
<li>点击可切换分页</li>
<li><strong>滑动可切换分页</strong></li>
<li>细节处理：<ol>
<li>点击文字变色</li>
<li>分页切换时指示器（紧挨着标签文字下方的小横条）也要跟着移动</li>
</ol>
</li>
</ol>
<p>所以在代码中我们也是紧紧围绕以上几点做文章：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> <span class="keyword">implements</span> <span class="title">View</span>.<span class="title">OnClickListener</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> ViewPager viewPager;</span><br><span class="line">    <span class="keyword">private</span> ArrayList&lt;View&gt; pageList;</span><br><span class="line">    <span class="keyword">private</span> TextView titleAll; <span class="comment">//标题文字“全部”</span></span><br><span class="line">    <span class="keyword">private</span> TextView titleNew; <span class="comment">//标题文字“新增”</span></span><br><span class="line">    <span class="keyword">private</span> ImageView indicator; <span class="comment">//滚动指示器</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> offset = <span class="number">0</span>; <span class="comment">//滚动条初始偏移量</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> currIndex = <span class="number">0</span>; <span class="comment">//当前页编号</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> bmpW; <span class="comment">//滚动条宽度</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> one; <span class="comment">//一倍滚动量</span></span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//开始绑定按钮</span></span><br><span class="line">        viewPager = (ViewPager) findViewById(R.id.viewPager);</span><br><span class="line">        titleAll = (TextView) findViewById(R.id.title_all);</span><br><span class="line">        titleNew = (TextView) findViewById(R.id.title_new);</span><br><span class="line">        indicator = (ImageView) findViewById(R.id.indicator);</span><br><span class="line">        <span class="comment">//用LayoutInflater来绑定布局</span></span><br><span class="line">        LayoutInflater inflater = getLayoutInflater();</span><br><span class="line">        View view1 = inflater.inflate(R.layout.page_item_all, <span class="keyword">null</span>);<span class="comment">//先预加载这两个页布局</span></span><br><span class="line">        View view2 = inflater.inflate(R.layout.page_item_new, <span class="keyword">null</span>);</span><br><span class="line">        pageList = <span class="keyword">new</span> ArrayList&lt;View&gt;();<span class="comment">//pageList被实例化为装载View元素的数组</span></span><br><span class="line">        pageList.add(view1);</span><br><span class="line">        pageList.add(view2);</span><br><span class="line">        <span class="comment">//add的先后顺序不要搞错，先add的就是array[0]位置的元素了</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//初始化分页适配器功能</span></span><br><span class="line">        PagerAdapter pagerAdapter = <span class="keyword">new</span> PagerAdapter() &#123;</span><br><span class="line">            <span class="comment">//参考：https://www.cnblogs.com/weixing/archive/2013/10/11/3363951.html</span></span><br><span class="line">            </span><br><span class="line">            <span class="comment">//获取页卡总数量</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getCount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> pageList.size();</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">//判断是否由对象生成界面，这个很重要，是用来把pageView数组中的page1和page2生成到当前布局中的方法</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isViewFromObject</span><span class="params">(View view, Object object)</span> </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> view == object;</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">//使从ViewGroup中移出当前View</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroyItem</span><span class="params">(ViewGroup arg0, <span class="keyword">int</span> arg1, Object arg2)</span> </span>&#123;</span><br><span class="line">                ((ViewPager) arg0).removeView(pageList.get(arg1));</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">//返回一个对象，这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> Object <span class="title">instantiateItem</span><span class="params">(ViewGroup arg0, <span class="keyword">int</span> arg1)</span> </span>&#123;</span><br><span class="line">                <span class="comment">//这个方法用来实例化页卡</span></span><br><span class="line">                ((ViewPager) arg0).addView(pageList.get(arg1));</span><br><span class="line">                <span class="keyword">return</span> pageList.get(arg1);</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="comment">//绑定适配器</span></span><br><span class="line">        viewPager.setAdapter(pagerAdapter);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//设置viewPager的初始界面为第一个页面</span></span><br><span class="line">        viewPager.setCurrentItem(<span class="number">0</span>); <span class="comment">//这里的0对应的是pageList[0]，也就是page1了</span></span><br><span class="line">        <span class="comment">//添加切换界面的监听器</span></span><br><span class="line">        viewPager.addOnPageChangeListener(<span class="keyword">new</span> MyOnPageChangeListener());</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//监听文字被点击</span></span><br><span class="line">        titleAll.setOnClickListener(<span class="keyword">this</span>);</span><br><span class="line">        titleNew.setOnClickListener(<span class="keyword">this</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//初始化指示器的各项参数</span></span><br><span class="line">        InitScrollbar(bmpW);</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">InitScrollbar</span><span class="params">(<span class="keyword">int</span> bmpW)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 获取滚动条指示器的宽度</span></span><br><span class="line">        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.scrollbar).getWidth();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//为了获取屏幕宽度，新建一个DisplayMetrics对象</span></span><br><span class="line">        DisplayMetrics displayMetrics = <span class="keyword">new</span> DisplayMetrics();</span><br><span class="line">        <span class="comment">//将当前窗口的一些信息放在DisplayMetrics类中</span></span><br><span class="line">        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);</span><br><span class="line">        <span class="comment">//从而得到屏幕的宽度</span></span><br><span class="line">        <span class="keyword">int</span> screenW = displayMetrics.widthPixels;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//初始化滚动条的位置</span></span><br><span class="line">        offset = screenW / <span class="number">2</span> - bmpW - <span class="number">34</span>;</span><br><span class="line">        Matrix matrix = <span class="keyword">new</span> Matrix();</span><br><span class="line">        matrix.postTranslate(offset, <span class="number">0</span>);</span><br><span class="line">        indicator.setImageMatrix(matrix);</span><br><span class="line">        <span class="comment">//计算出切换一个界面时，滚动条的位移量</span></span><br><span class="line">        one = bmpW + <span class="number">74</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//这里是文字被点击时的逻辑</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">switch</span> (view.getId()) &#123;</span><br><span class="line">            <span class="keyword">case</span> R.id.title_all:</span><br><span class="line">                <span class="comment">//当点击“全部”标签时，调用左分页的加载</span></span><br><span class="line">                viewPager.setCurrentItem(<span class="number">0</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> R.id.title_new:</span><br><span class="line">                <span class="comment">//当点击“新增”标签时，调用右分页的加载</span></span><br><span class="line">                viewPager.setCurrentItem(<span class="number">1</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//页面滚动监听器功能，实现标签页左右滑动切换效果</span></span><br><span class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyOnPageChangeListener</span> <span class="keyword">implements</span> <span class="title">ViewPager</span>.<span class="title">OnPageChangeListener</span> </span>&#123;</span><br><span class="line">        </span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPageSelected</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">            Animation animation = <span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">switch</span> (index) &#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">                    titleAll.setTextColor(<span class="number">0xff000000</span>);<span class="comment">//0x表示整型，ff表示透明度为0，最后的6位数字表示颜色，必须这样写，不能省略</span></span><br><span class="line">                    titleNew.setTextColor(<span class="number">0xff8e8e8e</span>);</span><br><span class="line">                    <span class="comment">//该情况下，指示器从右向左移动</span></span><br><span class="line">                    animation = <span class="keyword">new</span> TranslateAnimation(one, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">                    <span class="comment">//以上四个参数分别代表开始点离x坐标差值，结束点离x坐标差值；开始点离y坐标差值，结束点离y坐标差值</span></span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">                    titleNew.setTextColor(<span class="number">0xff000000</span>);</span><br><span class="line">                    titleAll.setTextColor(<span class="number">0xff8e8e8e</span>);</span><br><span class="line">                    <span class="comment">//该情况下，指示器从左向右移动</span></span><br><span class="line">                    animation = <span class="keyword">new</span> TranslateAnimation(<span class="number">0</span>, one, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            currIndex = index;<span class="comment">//使当前页编号等于该值，0表示“全部”页，1表示“新增”页</span></span><br><span class="line">            animation.setFillAfter(<span class="keyword">true</span>);<span class="comment">//将此值设置为true表示指示器将停止在最终移动到的位置上</span></span><br><span class="line">            <span class="comment">//动画持续时间，单位为毫秒</span></span><br><span class="line">            animation.setDuration(<span class="number">200</span>);</span><br><span class="line">            <span class="comment">//开始执行动画</span></span><br><span class="line">            indicator.startAnimation(animation);</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPageScrolled</span><span class="params">(<span class="keyword">int</span> arg0, <span class="keyword">float</span> arg1, <span class="keyword">int</span> arg2)</span> </span>&#123;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPageScrollStateChanged</span><span class="params">(<span class="keyword">int</span> arg0)</span> </span>&#123;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果你不喜欢代码中某些变量的命名，可以使用<code>shift+f6</code>批量修改。（详见：<a href="http://blog.csdn.net/Likianta/article/details/78612532）" target="_blank" rel="noopener">http://blog.csdn.net/Likianta/article/details/78612532）</a></p>
<p>完成后运行，看看效果是否符合预期（当时忘记截图了，下面这张图是包含了下章内容的截图，旧图中分页本该显示的是两种纯色背景）。<br><img src="http://ozurciydg.bkt.clouddn.com/17-11-23/12576326.jpg" alt="page1"><br><img src="http://ozurciydg.bkt.clouddn.com/17-11-23/45054082.jpg" alt="page2"></p>
<hr>
<h1 id="5-其他功能更新"><a href="#5-其他功能更新" class="headerlink" title="5. 其他功能更新"></a>5. 其他功能更新</h1><h2 id="5-1-软键盘按Enter键触发登录按钮"><a href="#5-1-软键盘按Enter键触发登录按钮" class="headerlink" title="5.1. 软键盘按Enter键触发登录按钮"></a>5.1. 软键盘按Enter键触发登录按钮</h2><p>上章制作的登录界面，大家在使用的时候会觉得每次点“进入”之前要先关闭软键盘应该很麻烦，所以在LoginActivity.java中新增一个检测软键盘“Enter”键的功能：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LoginActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">    <span class="comment">//监听软键盘输入，当输入Enter的时候，也能触发上面的登录按钮</span></span><br><span class="line">    etPassword.setOnKeyListener(<span class="keyword">new</span> View.OnKeyListener() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onKey</span><span class="params">(View view, <span class="keyword">int</span> i, KeyEvent keyEvent)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (i == keyEvent.KEYCODE_ENTER) &#123;</span><br><span class="line">                loginOn.callOnClick(); <span class="comment">//callOnClick是performClick的简化版，适合简单触发点击监听事件</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">            <span class="comment">/**关于返回值的说明</span></span><br><span class="line"><span class="comment">             * 以煮面来举例，返回值为true，表示“这块面交给我了，系统你不要再过问此事”。比如键盘输入“1”，让它发出鸟叫声。则编辑框中不会出现数字1，因为系统不去管这个键了</span></span><br><span class="line"><span class="comment">             * 返回值为false，则表示“我过来给这块面加点调料，完了系统你继续煮”。比如键盘输入“1”，不仅能发出鸟叫声，编辑框中也会出现数字1，因为系统要按自己的步伐去执行</span></span><br><span class="line"><span class="comment">             * 更多解析见此回答：https://zhidao.baidu.com/question/1430105248859125459.html</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="5-2-将颜色设置为通用的色卡并放到colors-xml文件中"><a href="#5-2-将颜色设置为通用的色卡并放到colors-xml文件中" class="headerlink" title="5.2. 将颜色设置为通用的色卡并放到colors.xml文件中"></a>5.2. <span id="jump1">将颜色设置为通用的色卡并放到colors.xml文件中</span></h2><p>（<a href="#return1">返回原位置</a>）<br>界面中经常会重复用到几种固定的颜色，可以将其定义到<code>res/values/colors.xml</code>文件中：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"colorPrimary"</span>&gt;</span>#3F51B5<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"colorPrimaryDark"</span>&gt;</span>#303F9F<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"colorAccent"</span>&gt;</span>#FF4081<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">&lt;!--自制色卡--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"mycolorLineShadow"</span>&gt;</span>#868686<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"mycolorText1"</span>&gt;</span>#000000<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"mycolorText2"</span>&gt;</span>#8e8e8e<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"mycolorPurple"</span>&gt;</span>#6000c0<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"mycolorPurpleInactive"</span>&gt;</span>#8668a5<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"mycolorBackground"</span>&gt;</span>#dedede<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>使用方法：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:textColor</span>=<span class="string">"@color/mycolorText1"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">...</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">TextView</span>&gt;</span></span><br></pre></td></tr></table></figure>
<hr>
<h1 id="相关参考"><a href="#相关参考" class="headerlink" title="相关参考"></a>相关参考</h1><ol>
<li>Android实现界面左右滑动切换 - Black<strong>Lotus的博客 - CSDN博客 <a href="http://m.blog.csdn.net/black" target="_blank" rel="noopener">http://m.blog.csdn.net/black</a></strong>lotus/article/details/50805389?yyue=a21bo.50862.201879</li>
<li>Aandroid在ViewPager中添加ListView_zero零零 <a href="http://blog.sina.cn/dpool/blog/s/blog_95bc05000101bhxe.html" target="_blank" rel="noopener">http://blog.sina.cn/dpool/blog/s/blog_95bc05000101bhxe.html</a></li>
<li><a href="https://zhidao.baidu.com/question/1430105248859125459.html" target="_blank" rel="noopener">https://zhidao.baidu.com/question/1430105248859125459.html</a></li>
</ol>
<hr>
<h1 id="日志"><a href="#日志" class="headerlink" title="日志"></a>日志</h1><p><strong>2017年11月17日</strong></p>
<ol>
<li><strong>【创建】主页面</strong></li>
<li>【修改】解决线条位置的错误</li>
</ol>
<p><strong>2017年11月20日</strong></p>
<ol>
<li><strong>【新增】主页面的左右滑动效果</strong></li>
<li>【修改】将颜色设置为通用的色卡并放到colors.xml文件中</li>
<li>【修改】软键盘按Enter键触发登录按钮</li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/1123112036/" rel="next" title="Android Studio批量重命名快捷键">
                <i class="fa fa-chevron-left"></i> Android Studio批量重命名快捷键
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/1123205552/" rel="prev" title="03 Anykey左分页布局RecyclerView">
                03 Anykey左分页布局RecyclerView <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNDI4MS8xMDgxOA=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://ozurciydg.bkt.clouddn.com/18-2-12/72826549.jpg"
                alt="Likianta" />
            
              <p class="site-author-name" itemprop="name">Likianta</p>
              <p class="site-description motion-element" itemprop="description">Android Dev</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">73</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="http://blog.csdn.net/likianta" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-crosshairs"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:sheerish@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/likianta" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5441767757/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>Weibo</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-先看一下最终的效果图"><span class="nav-text">1. 先看一下最终的效果图</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-制作主界面layout-main-xml"><span class="nav-text">2. 制作主界面layout_main.xml</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-然后制作两个分页"><span class="nav-text">3. 然后制作两个分页</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-MainActivity代码"><span class="nav-text">4. MainActivity代码</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-其他功能更新"><span class="nav-text">5. 其他功能更新</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#5-1-软键盘按Enter键触发登录按钮"><span class="nav-text">5.1. 软键盘按Enter键触发登录按钮</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-2-将颜色设置为通用的色卡并放到colors-xml文件中"><span class="nav-text">5.2. 将颜色设置为通用的色卡并放到colors.xml文件中</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#相关参考"><span class="nav-text">相关参考</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#日志"><span class="nav-text">日志</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Likianta</span>

  
</div>


  <div class="powered-by">
    <i class="fa fa-user-md"></i>
    <span id="busuanzi_container_site_pv">
      Site visitors:<span id="busuanzi_value_site_pv"></span>
    </span>
  </div>

<!--
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.3</div>

-->



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">Site words total count:75.2k</span>
</div>
        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Evernote";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  

  

  

</body>
</html>
